<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
class HostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        
        
         // $users_id = $request->session()->get('user_id');
       
       // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
           
        $host = DB::table('hos_list')
                    ->orderBy('created_at','DESC')
                    ->get();
        return view('admin.host.index',['list'=>$host]);
        
       
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $list = \DB::select("select * from type  order by concat(path,id) asc");
        
        //处理信息
      foreach($list as &$v){
                $m = substr_count($v->path,","); //获取path中的逗号
                //生成缩进
                $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
            
            
        return view('admin.host.create',['list'=>$list]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
            // $users_id = $request->session()->get('user_id');
       
           // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
            
            //获取指定的部分数据
            $data['hos_name'] = $request->input('product_name');
            $data['province'] = $request->input('price');
            if(!$request->hasFile('pic')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('pic');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['hos_img'] = $img->store(date('Ymd'));
            
            
            if(!$request->hasFile('p_img')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('p_img');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['hos_linimg'] = $img->store(date('Ymd'));
            
            $data['created_at'] = date("Y-m-d H:i:s");
            
             $id = \DB::table("hos_list")->insertGetId($data);
        
           if($id>0){
                 return redirect('host');
             }else{
                return back()->with("err","添加失败!");
          } 
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
